package com.tqz.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.tqz.model.Article;

/**
 * 数据操作类
 * 
 * @author 断片先生
 *
 */
public class NewsSerch_army {
	/**
	 * 获取所有文章
	 */
	public List<Article> getAllArticles() {
		// 建立集合
		List<Article> list = new ArrayList<>();

		// 建立连接、会话、结果集
		Connection connection = DBHlper.getConnection();
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		try {
			// 连接到数据库，并开启会话
			statement = (PreparedStatement) connection.prepareStatement("select * from article5");
			// 执行会话，并建立结果集
			resultSet = statement.executeQuery();
			// 遍历结果集，取出数据库里面的信息，并组成实体类
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String title = resultSet.getString("title");
				String content = resultSet.getString("content");
				int weight = resultSet.getInt("weight");
				// 遍历一组数据就new一个对象出来。
				Article article = new Article(id, title, content, weight);
				// 将获取到的实体类信息存入集合
				list.add(article);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBHlper.close(connection, statement, resultSet);
		}
		return list;
	}

	/**
	 * 根据id获取单篇文章
	 */
	public Article getArticle(int id) {
		// 建立对象
		Article article = new Article();
		// 建立连接、会话、结果集
		Connection connection = DBHlper.getConnection();
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		try {
			// 连接到数据库，并开启会话
			statement = (PreparedStatement) connection.prepareStatement("select * from article5 where id=?");
			statement.setInt(1, id);
			// 执行会话，并建立结果集
			resultSet = statement.executeQuery();
			while (resultSet.next()) {
				article.setId(resultSet.getInt("id"));
				article.setTitle(resultSet.getString("title"));
				article.setContent(resultSet.getString("content"));
				article.setWeight(resultSet.getInt("weight"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBHlper.close(connection, statement, resultSet);
		}
		return article;
	}

	/**
	 * 删除指定标题文章
	 */
	public boolean deleteIdArticle(String title) {
		// 建立连接
		Connection connection = DBHlper.getConnection();
		// 开启会话
		PreparedStatement statement = null;
		try {
			statement = (PreparedStatement) connection.prepareStatement("delete from article5 where title=?");
			statement.setString(1, title);
			statement.execute();
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} finally {
			// 关闭会话 和 连接
			try {
				statement.close();
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return true;
	}

	/**
	 * 添加文章
	 */
	public int addArticle(String title, String content, int weight) {
		int affect = 0;
		// 建立连接
		Connection connection = DBHlper.getConnection();
		// 开启会话
		PreparedStatement statement = null;
		try {
			statement = (PreparedStatement) connection
					.prepareStatement("insert into article5(title,content,weight) values(?,?,?)");
			statement.setString(1, title);
			statement.setString(2, content);
			statement.setInt(3, weight);
			affect = statement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			// 关闭会话 和 连接
			try {
				statement.close();
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return affect;
	}

	/**
	 * 更改文章
	 */
	public int upDateArticle(String title, String content, int weight) {
		int affect = 0;
		// 建立连接
		Connection connection = DBHlper.getConnection();
		// 开启会话
		PreparedStatement statement = null;
		try {
			statement = (PreparedStatement) connection
					.prepareStatement("update article5 set content=?,weight=? where title=?");

			statement.setString(1, content);
			statement.setInt(2, weight);
			statement.setString(3, title);

			affect = statement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			// 关闭会话 和 连接
			try {
				statement.close();
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return affect;
	}
}
